import os
import sys

import imp

__all__ = ['we_are_frozen', 'mymodule', 'main_is_frozen', 'mymain']

if sys.version_info[0] >= 3:
    def _u(s, *arg):
        return s
else:
    def _u(s, *arg):
        return unicode(s, *arg)

def we_are_frozen():
    """Returns whether we are frozen via py2exe.
    This will affect how we find out where we are located."""
    return hasattr(sys, "frozen")

def mymodule():
    """ This will get us the program's directory,
    even if we are frozen using py2exe"""
    if we_are_frozen():
        return os.path.dirname(_u(sys.executable, sys.getfilesystemencoding()))
    return os.path.dirname(_u(__file__, sys.getfilesystemencoding()))


def main_is_frozen():
   return (hasattr(sys, "frozen") # new py2exe
           or hasattr(sys, "importers") # old py2exe
           or imp.is_frozen("__main__")) # tools/freeze

def mymain():
   if main_is_frozen():
       return os.path.dirname(sys.executable)
   return os.path.dirname(sys.argv[0])